package mock.appcode.dao.daointerface;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import mock.appcode.common.valueobjects.ProgrammeVO;

/**
 * 
 * @author TrangPDD
 * 
 */
public class ProgrammeDAOImpl extends BaseDAOImpl implements BaseDAO {
	
	Connection con = null;
	ProgrammeVO programmeVo;

	public ArrayList<ProgrammeVO> getListProgramme() {
		ArrayList<ProgrammeVO> l = new ArrayList<ProgrammeVO>();
		Statement st;
		ResultSet rs;
		String sql = "Select * from programme";
		try {
			con = getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int idPro = rs.getInt(1);
				String name = rs.getString(2);
				String des = rs.getString(3);
				boolean isActive = rs.getBoolean(4);
				int idContact = rs.getInt(5);
//				LoginAction a = new LoginAction(acc, first, last, email);
				programmeVo.setIdProgramme(idPro);
				programmeVo.setName(name);
				programmeVo.setDescription(des);
				programmeVo.setActive(isActive);
				programmeVo.setIdContact(idContact);
				l.add(programmeVo);
			}
		} catch (Exception e) {
		}
		return l;
	}

	public boolean isExist(String acc) {
//		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM account WHERE acc_name = ?";
		try {
			con = super.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, acc);
			rs = ps.executeQuery();
			return rs.next();

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(),
					"Database Error", JOptionPane.ERROR_MESSAGE);
			return false;
			// System.exit(1);
		}
	}

	public boolean addNew(String acc, String first, String last, String email) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "INSERT INTO account VALUES (?, ?, ?, ?)";
		try {
			con = super.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, acc);
			ps.setString(2, first);
			ps.setString(3, last);
			ps.setString(4, email);
			return ps.execute();

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(),
					"Database Error", JOptionPane.ERROR_MESSAGE);
			// System.exit(1);
		}
		return false;
	}

	public boolean update(String acc, String first, String last, String email) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "UPDATE account SET first_name = ?, last_name = ?, email = ? WHERE acc_name = ?";
		try {
			con = super.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, first);
			ps.setString(2, last);
			ps.setString(3, email);
			ps.setString(4, acc);
			return ps.execute();

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(),
					"Database Error", JOptionPane.ERROR_MESSAGE);
			// System.exit(1);
		}
		return false;
	}

}
